#include "mytitlebar.h"
#include <QHBoxLayout>
#include <QButtonGroup>
#include <QPainter>
#include <QLabel>
MyTitleBar::MyTitleBar(QWidget *parent)
    : QWidget(parent)
    , m_pBtnGroup(nullptr)
    , m_pAddBtn(nullptr)
    , m_pminimizeButton(nullptr)
    , m_pmaximizeButton(nullptr)
    , m_pcloseButton(nullptr)
{
    m_pMainlayout = new QHBoxLayout(this);
    m_pMainlayout->setContentsMargins(0,0,0,0);
    MyBtnBarTitle* pBarTitle = new MyBtnBarTitle(this);
    pBarTitle->setMinimumHeight(30);
    m_pMainlayout->addWidget(pBarTitle);

    m_pBtnGroup = new QButtonGroup(this);
    m_pAddBtn = new QPushButton("+", this);
    connect(m_pAddBtn, &QPushButton::clicked, this, &MyTitleBar::sigAddWeb);

    m_pBtnGroup->addButton(pBarTitle);
    m_pBtnGroup->addButton(m_pAddBtn);
    m_pMainlayout->addWidget(m_pAddBtn);
    m_pMainlayout->addStretch();

    m_pminimizeButton = new QPushButton("-", this);
    m_pMainlayout->addWidget(m_pminimizeButton);

    m_pmaximizeButton = new QPushButton("□", this);
    m_pMainlayout->addWidget(m_pmaximizeButton);

    m_pcloseButton = new QPushButton("X", this);
    m_pMainlayout->addWidget(m_pcloseButton);

    setLayout(m_pMainlayout);
    setStyleSheet("background-color: #30a5a7; color: black;");

    connect(m_pminimizeButton, &QPushButton::clicked, this, &MyTitleBar::minimize);
    connect(m_pmaximizeButton, &QPushButton::clicked, this, &MyTitleBar::toggleMaximize);
    connect(m_pcloseButton, &QPushButton::clicked, this, &MyTitleBar::close);
}

MyTitleBar::~MyTitleBar()
{

}

void MyTitleBar::addTitleBar()
{
    MyBtnBarTitle* pBarTitle = new MyBtnBarTitle(this);
    int width = pBarTitle->width();
    pBarTitle->setMinimumHeight(30);
    m_pMainlayout->insertWidget(1, pBarTitle);
    m_pBtnGroup->addButton(pBarTitle);
    resizeTtitle();
}

void MyTitleBar::resizeTtitle()
{
    QList<QAbstractButton*> btnsList = m_pBtnGroup->buttons();
    int nCurrnetWidth = 0;
    for(int i = 0; i < btnsList.count(); i++)
    {
        nCurrnetWidth += btnsList[i]->width();
    }
        //可展示的空间
    int nSpaceWidth = width() - m_pminimizeButton->width()
                      - m_pmaximizeButton->width() - m_pcloseButton->width();
    if(nCurrnetWidth > nSpaceWidth)
    {
        int nOneWidth = nSpaceWidth / btnsList.count();
        for(int i = 0; i < btnsList.count(); i++)
        {
            if(i == btnsList.count() - 1)
            {
                return;
            }
            btnsList[i]->resize(nOneWidth, btnsList[i]->height());
        }
    }
}

MyBtnBarTitle::MyBtnBarTitle(QWidget *parent)
    :QPushButton(parent)
    ,m_qsTitle("")

{
    QLabel* pLabel = new QLabel(this);
    pLabel->setText(tr("百度一下，你就知道"));
    pLabel->setAutoFillBackground(false);
    QHBoxLayout* mainlayout = new QHBoxLayout(this);
    mainlayout->addWidget(pLabel);
    mainlayout->addStretch();
    QPushButton* btn = new QPushButton(this);
    btn->setText("X");
    mainlayout->addWidget(btn);
    setCheckable(true);
    setStyleSheet("QPushButton:checked { background-color: #62d1d2;}");


    this->setLayout(mainlayout);
}

MyBtnBarTitle::~MyBtnBarTitle()
{

}

void MyBtnBarTitle::setTitleText(const QString &strTitle)
{

}

// void MyBtnBarTitle::paintEvent(QPaintEvent *event)
// {
//     // QPainter painter(this);
//     // //获取字体大小看看能不能放下
//     // QFontMetrics metrics(painter.font());
//     // int textWidth = metrics.boundingRect(m_qsTitle).width();
//     // if(textWidth < )
//     // {

//     // }
//     //painter.drawText(,);

// }
